package com.example.demo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author CHAS
 * 2024/8/1 23:42
 **/
@Service
@Slf4j
public class RabbitmqServiceImpl {

    private static int count = 0;
    @Resource
    private UserMapper userMapper;

    @Autowired
    private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;

//    @RabbitListener(queues = "work_zs_queue",id="work_zs_queue")
////    @SneakyThrows
//    public void workc1(User user){
//        count++;
//        try {
////            user = userMapper.selectById(2);
//            userMapper.insert(user);
//        } catch (Exception e) {
//            log.error("测试restTemplate失败", e);
//            e.printStackTrace();
//        }
//        System.out.println("workc1:---------------->"+count+"---->"+ JSON.toJSONString(user));
//    }

    public void stopListener() {
        this.rabbitListenerEndpointRegistry.getListenerContainer("work_zs_queue").stop();
    }

    public void startListener() {
        this.rabbitListenerEndpointRegistry.getListenerContainer("work_zs_queue").start();
    }
}
